package com.minivision.fdiot.mqtt.message;

import java.nio.ByteBuffer;
import java.util.Date;


import com.minivision.fdiot.protopool.BinaryBody;
import com.minivision.fdiot.protopool.CodecException;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class BinaryStatus extends BinaryBody {
  
  private static final long serialVersionUID = -4324348301179159025L;
  private float cpu;
  private float mem;
  private Date timestamp;
  private byte[] imgBytes;
  
  @Override
  public byte[] encode() throws CodecException {
    ByteBuffer buffer = ByteBuffer.allocate(16 + imgBytes.length);
    buffer.putFloat(cpu);
    buffer.putFloat(mem);
    buffer.putLong(timestamp.getTime());
    buffer.put(imgBytes);
    return buffer.array();
  }

  @Override
  public BinaryBody decode(byte[] bytes) throws CodecException {
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    this.cpu = buffer.getFloat();
    this.mem = buffer.getFloat();
    this.timestamp = new Date(buffer.getLong());
    buffer.position(16);
    imgBytes = new byte[buffer.remaining()];
    buffer.get(imgBytes);
    return this;
  }

}
